package tp.pr2.mv;

import tp.pr2.mv.instruction.Instruction;

public class ProgramMV {
	private Instruction[] listainst;
	private int contador; // Contador de programa
	private int size = 10;
	
	public ProgramMV() {
		this.listainst = new Instruction[size];
		this.contador = 0;
	}
	
	public int getContadorProgramMv() {
		return this.contador;
	}
	
	public Instruction getListainst(int x) {
		Instruction instrOut;
		//Para devolver una instruction comprueba que este dentro del rango del contador
		if(x >= this.contador)
			instrOut = null;
		else
			instrOut = this.listainst[x];
		
		return instrOut;
	}
	
	public void nuevaInst(Instruction inst) {
		if (listaLlena()) resize();
		this.listainst[this.contador] = inst;
		this.contador++;
	}
	
	private boolean listaLlena() {
		return (this.contador == this.size);
	}
	
	private void resize() {
		if (listaLlena()) {
			int tamanio = 2;
			this.size = this.size*tamanio; // Aumenta el tamanio en multiplos de diez.
			Instruction [] listaaux = listainst;
			this.listainst = new Instruction[this.size];
			for (int i = 0; i < this.contador; i++)
				this.listainst[i] = listaaux[i];
			tamanio++;
		}
	}
	
	public String toString() {
		String cadena = "";
		for (int i = 0; i < contador; i++) {
			cadena = cadena + i + ": " + listainst[i] + '\n';
		}
		return cadena;
	}
}